package com.share.thread;

/**
 * 
 * 系统设计描述： 简单模拟一下火车票的售票系统(为使程序简单，我们就抽出最简单的模型进行模拟)：
 * 有500张从成都到乐山的火车票，在8个窗口同时出售，保证系统的稳定性和数据的一致性。
 * 
 * 服务器类 @Service
 * 售票类 @TicketSaler
 * 售票系统类 @TicketingSystem
 * 
 * 
 */
public class TicketingSystem {

	public static void main(String[] args) {

		Service service = new Service("成都-->乐山", 100);//
		TicketSaler ticketSaler = new TicketSaler("售票程序", service);

		// 创建8个线程，以模拟8个窗口
		Thread threads[] = new Thread[8];

//		for (int i = 0; i < threads.length; i++) {
//			threads[i] = new Thread(ticketSaler, "窗口" + (i + 1));
//			System.out.println("窗口" + (i + 1) + "开始出售 " + service.getTicketName() + " 的票...");
//			threads[i].start();
//		}

	}

}

/**
 * 模拟服务器的类
 */
class Service {
	private String ticketName; // 票名
	private int totalCount; // 总票数
	private int remaining; // 剩余票数

	public Service(String ticketName, int totalCount) {
		this.ticketName = ticketName;
		this.totalCount = totalCount;
		this.remaining = totalCount;
	}

	public synchronized int getRemaining() {
		return remaining;
	}

	public String getTicketName() {
		return this.ticketName;
	}

//	public synchronized int saleTicket(int ticketNum) {
//		if (remaining > 0) {
//			remaining -= ticketNum;
//			try { // 暂停0.1秒，模拟真实系统中复杂计算所用的时间
//				Thread.sleep(100);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//
//			if (remaining >= 0) {
//				return remaining;
//			} else {
//				//remaining += ticketNum;
//				return -1;
//			}
//		}
//		return -1;
//	}
}

/**
 * 售票程序
 */
class TicketSaler implements Runnable {
	private String name;
	private Service service;

	public TicketSaler(String windowName, Service service) {
		this.name = windowName;
		this.service = service;
	}

	@Override
	public void run() {
		while (service.getRemaining() > 0) {
//			synchronized (this) {
//				System.out.print(Thread.currentThread().getName() + "出售第" + service.getRemaining() + "张票 ，");
//				int remaining = service.saleTicket(1);
//				if (remaining >= 0) {
//					System.out.println("出票成功!剩余" + remaining + "张票.");
//				} else {
//					System.out.println("出票失败！该票已售完。");
//				}
//			}
		}
	}
}
